<?php

namespace app\queue\rabbitmq;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

abstract class RabbitBase
{
    protected $connection;
    protected $channel;

    public function __construct()
    {
        $config = require __DIR__ . '/../../config/rabbitmq.php';
        $this->connection = new AMQPStreamConnection(
            $config['host'],
            $config['port'],
            $config['user'],
            $config['password'],
            $config['vhost']
        );
        $this->channel = $this->connection->channel();

        // publish confirm mode
        $this->channel->confirm_select();

        // send success
        $this->channel->set_ack_handler(function (AMQPMessage $message) {
            $data = json_decode($message->body);
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        });

        // send fail
        $this->channel->set_nack_handler(function (AMQPMessage $message) {
            $data = json_decode($message->body);
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        });
    }

    public function __destruct()
    {
        $this->channel->close();
        $this->connection->close();
    }

    public function send()
    {
        
    }

    public function receive()
    {

    }

    public function delaySend()
    {

    }

    public function delayReceive()
    {

    }
}